package com.atguigu.guli.service.edu.service.impl;

import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.CourseDescription;
import com.atguigu.guli.service.edu.entity.form.CourseInfoForm;
import com.atguigu.guli.service.edu.entity.query.CourseApiQuery;
import com.atguigu.guli.service.edu.entity.vo.CourseDetailsVo;
import com.atguigu.guli.service.edu.entity.vo.CourseInfoVo;
import com.atguigu.guli.service.edu.mapper.CourseDescriptionMapper;
import com.atguigu.guli.service.edu.mapper.CourseMapper;
import com.atguigu.guli.service.edu.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-06-27
 */
@Service
@Transactional
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Autowired
    CourseDescriptionMapper courseDescriptionMapper;
    @Override
    public String saveCourseInfo(CourseInfoForm courseInfo) {
        Course course = new Course();
        // 如果两个bean的属性名和数据类型都一致，可以直接将对象的属性值拷贝给另外一个类的对象
        BeanUtils.copyProperties(courseInfo , course);
        baseMapper.insert(course);
        String id = course.getId();//获取新增成功的课程的id

        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseInfo.getDescription());
        //我们希望自己手动设置课程描述的id 使用课程的id
        courseDescription.setId(id);
        courseDescriptionMapper.insert(courseDescription);
        return id;
    }

    @Override
    public Page<CourseInfoVo> getCourseInfoVos(Integer pageNum, Integer pageSize) {
        Page<CourseInfoVo> page = new Page(pageNum , pageSize);
        //查询 Page需要的分页数据集合
        // limit  index, pageSize     total:总记录条数
        // mp: 只需要在自定义的mapper的方法中  传入一个page对象，携带pageNUm和pageSize参数值，mp会自动使用分页拦截器使用它的条件
        QueryWrapper<CourseInfoVo> queryWrapper = new QueryWrapper();
//        queryWrapper.eq("1" , "1");
        queryWrapper.orderByDesc("t1.gmt_create");
        List<CourseInfoVo> courseInfoVos = baseMapper.getCourseInfoVos(page,queryWrapper);

        page.setRecords(courseInfoVos);//将查询到的分页数据集合设置给page对象
        return page;
    }

    @Override
    public CourseInfoVo getPublishCourse(String courseId) {
        //由于以后多表联查自定义sql 涉及的字段从多表中获取，不能使用LambdaQueryWrapper 自动分析javabean的属性名映射的表的字段名
        QueryWrapper<CourseInfoVo> queryWrapper = new QueryWrapper();
        queryWrapper.eq("t1.id" , courseId);  //根据课程id查询课程详情
        return baseMapper.getCourseInfoVoByCourseId(queryWrapper);
    }

    @Override
    public List<Course> listCourses(CourseApiQuery courseApiQuery) {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        String subjectId = courseApiQuery.getSubjectId();
        String subjectParentId = courseApiQuery.getSubjectParentId();
        Integer type = courseApiQuery.getType();
        String orderByColumn = courseApiQuery.getOrderByColumn();
        //用户只能访问已发布的课程
//        queryWrapper.eq("status" , "Normal");

        if(!StringUtils.isEmpty(subjectParentId)){
            queryWrapper.eq("subject_parent_id" , subjectParentId);
        }
        if(!StringUtils.isEmpty(subjectId)){//筛选条件
            queryWrapper.eq("subject_id" , subjectId);
        }
        if(!StringUtils.isEmpty(orderByColumn)){//排序字段
            if(type==null || type==0){
                //orderByColumn 传入的值必须和数据库要求排序的列名一样
                queryWrapper.orderByDesc(orderByColumn);

            }else{
                queryWrapper.orderByAsc(orderByColumn);

            }

        }
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public CourseDetailsVo getCourseDetails(String courseId) {
        CourseDetailsVo courseVo = baseMapper.getCourseDetailsByCourseId(courseId);
        courseVo.setViewCount(courseVo.getViewCount()+1);
        UpdateWrapper<Course> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",courseId);
        updateWrapper.set("view_count", courseVo.getViewCount());
        this.update(updateWrapper);
        return courseVo;
    }
}
